<template>
	<view>
		<view class="topnav">
			<!-- <uni-nav-bar :fixed="true" title="小说" leftIcon="left" @clickLeft="clickLeft"></uni-nav-bar> -->
			<uni-nav-bar :fixed="true" title="小说"></uni-nav-bar>
		</view>
		<view class="sectionBox">
			<uni-section title="文字滚动" subTitle="使用 scrollable 属性使通告滚动,此时 single 属性将失效,始终单行显示" type="line">
				<uni-notice-bar show-icon scrollable show-close text="下拉刷新可随机推荐,只有添加收藏的书籍才会实时更新章节哦" />
			</uni-section>
		</view>
		<view class="searchBox" v-show="searchIsShow">
			<!-- 基本用法 -->
			<view class="selectBox" >
				<uni-data-select
				      v-model="value"
				      :localdata="range"
					  :clear="false"
				    ></uni-data-select>
			</view>
			<view class="" style="width: 700rpx;">
				<uni-search-bar  @confirm="search" @input="input"></uni-search-bar>
			</view>
		</view>
		<view class="categoryBox">
			<uni-section title="基础分组" type="line">
				<uni-group title="小说">
					<view class="filterBox" >
						<view class="" >
							<h5 @click="filterBox = !filterBox"><text>筛选<i class="iconfont icon-shaixuan"></i></text>
								<text space="emsp">   {{fictionType}}</text>
							</h5>
						</view>
						<view class="filterContent" v-show="filterBox">
							<view class="filterMain">
								<view class="filterText"  >
									<text @click="selectFiction(null)">
										全部
									</text>
									<text v-for="(fiction,index) in LocalfictionJsons" @click="selectFiction(fiction)">
										{{fiction}} 
										<text v-show="(index+1)%7==0"><br></text> 
									</text>
								</view>
							</view>
						</view>
					</view>
					<view class="">
						<!-- {{total}} -->
					</view>
					<uni-list>
						<uni-list v-show="bookList">
							<!-- 自定义右侧内容 -->
							<uni-list-chat :title="item.title" v-for="(item,index) in bookList" :avatar="item.cover"
								:note="item.descs" time="2020-02-02 20:20"  :author="item.author"
								:fictionType="item.fictionType" :clickable="true" @click="goPage(item)">
								<view class="chat-custom-right">
									<text class="chat-custom-text">{{item.updateTime.substring(0,10)}}</text>
									<!-- 需要使用 uni-icons 请自行引入 -->
									<!-- <uni-icons type="star-filled" color="#999" size="18"></uni-icons> -->
									<!-- <span class="iconfont icon-yidu" style="color: #00ff00;"></span> -->
								</view>
							</uni-list-chat>
						</uni-list>
						<view v-show="!bookList">
							<text>暂无数据</text>
						</view>
					</uni-list>
				</uni-group>
			</uni-section>
		</view>
		<view class="">
			<uni-load-more :status="moreMsg"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import {
		getRandomChineseWord
	} from "../../../compoment/index.js"
	import * as DB from "@/compoment/DB.js"
	import fictionJsons from "@/compoment/bookType.json"
	export default {
		data() {
			return {
				bookList: [],
				key: "",
				page: 1,
				total: 0,
				moreMsg: "more",
				fictionType:undefined,
				searchIsShow:true,
				LocalfictionJsons:[],
				value: "title",
				range: [
					{ value: "title", text: "小说" },
					{ value: "author", text: "作者" }
				],
				filterBox:false
			}
		},
		onShow() {
			this.loadLocalFictionTypeJson()
		},
		onLoad(option) {
			this.fictionType = option.fictionType
			this.searchIsShow = this.fictionType?false:true
			this.search()
		},
		onPullDownRefresh() {
			this.page = 1
			this.bookList = []
			this.search()
		},
		onReachBottom() {
			this.page += 1
			this.search()
		},
		methods: {
			selectFiction(fiction){
				this.bookList = []
				this.fictionType = fiction
				this.page = 1
				this.search()
			},
			loadLocalFictionTypeJson() {
			  this.LocalfictionJsons = fictionJsons.fictionType
			},
			clickLeft() {
				uni.navigateBack()
			},
			search() {
				this.searchBook()
			},
			input(key) {
				this.bookList = []
				this.key = key
			},
			goPage(item){
				uni.navigateTo({
					url:"/pages/index/book/chapter/chapter?item="+JSON.stringify(item) 
				})
			},
			searchBook() {
				this.moreMsg = "loading"
				let type = this.fictionType?'fictionType':this.value
				let key = this.fictionType?this.fictionType:this.key || getRandomChineseWord()
				uni.request({
					url: `http://lry.pingcc.cn:9010/fiction/search/${type}/${key}/${this.page}/10`,
					method: "GET",
					success: ({
						data
					}) => {
						this.moreMsg="loading"
						if (!data.data) {
							this.moreMsg = "no-more"
						}
						this.bookList.push(...data.data)
						this.moreMsg = "no-more"
						this.total += data.count
						this.filterBox = false
						this.saveCategory(data.data)
						//添加更新书籍信息到服务器的数据库中
						this.putBooks(data.data);
					},
					complete() {
						uni.stopPullDownRefresh()
					}
				})
			},
			saveCategory(bookList){
				if(!bookList){
					return false
				}
				let categoryList = []
				DB.find("uu_bookCategory").then((res)=>{
					res.forEach(c =>{
						categoryList.push(c.fictionType)
					})
					bookList.forEach(item=>{
						categoryList.push(item.fictionType)
					})
					categoryList =  Array.from(new Set(categoryList))
					// s删除所有
					DB.remove("uu_bookCategory").then(()=>{
						let i = 1;
						categoryList.forEach(fictionType=>{
							DB.insert("uu_bookCategory",{'id':i,'fictionType':fictionType}).then((res)=>{
							}).catch((err)=>{
								console.log(err)
							})
							i++;
						})
					})
				}).catch((err)=>{
					console.log(err)
				})
				
			},
			putBooks(books){
				uni.request({
					url:"http://192.168.205.104:8900/book/putBooks",
					method:"POST",
					data:books,
					success: (res) => {
						console.log(res)
					}
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	image {
		will-change: transform
	}

	.chat-custom-right {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
	}

	.chat-custom-text {
		font-size: 12px;
		color: #999;
	}

	.image {
		width: 25px;
		height: 25px;
	}

	.text {
		font-size: 14px;
		margin-top: 5px;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		// display: block;
		/* #endif */
	}

	.grid-dynamic-box {
		margin-bottom: 15px;
	}

	.grid-item-box {
		flex: 1;
		// position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.grid-item-box-row {
		flex: 1;
		// position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.grid-dot {
		position: absolute;
		top: 5px;
		right: 15px;
	}

	.swiper {
		height: 420px;
	}

	/* #ifdef H5 */
	@media screen and (min-width: 768px) and (max-width: 1425px) {
		.swiper {
			height: 630px;
		}
	}

	@media screen and (min-width: 1425px) {
		.swiper {
			height: 830px;
		}
	}

	/* #endif */
	.searchBox{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}
	.filterBox{
		position: relative;
		h5{
			font-weight: 300;
			font-size: 20rpx;
			font-style: normal;
			padding: 2px 10rpx;
			font-display:auto;
		}
		.filterContent{
			position: absolute;
			top: 24rpx;
			left: 10rpx;
			background-color: #383838;
			opacity: 0.8;
			z-index: 100;
			width: 750rpx;
			height: 400rpx;
			overflow-y: auto;
			.filterMain{
				.filterText{
					text{
						font-size: 13px;
						font-weight: 350;
						color: #e8e8e8;
						padding: 2rpx 4rpx;
					}
				}
				.filterText>text:hover{
					color: #d1d1d1;
				}
			}
		}
	}
</style>
